//
//  BaseListViewController.m
//  CharonTVProject
//
//  Created by karong on 2023/6/6.
//

#import "BaseListViewController.h"
#define WSIs32BitCPU      (sizeof(void*) == 4)
@interface BaseListViewController ()

@property (nonatomic, assign) BOOL showLoading;   // 是否展示的是加載頁面
@property (nonatomic, assign) BOOL firstLoadPage; // 是否是首次加載頁面，防止一进入頁面就顯示空狀態
@property (nonatomic, strong) NSMutableDictionary *emtpyStateTextDict; // 空状态文字字典
@property (nonatomic, strong) NSMutableDictionary *emtpyStateImageDict; // 空状态图片字典
@property (nonatomic, strong) MJRefreshGifHeader *mj_header;
// 重试
@property (assign, nonatomic) BOOL retryRefresh;

@end

@implementation BaseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstLoadPage = YES;
    self.listView.showsHorizontalScrollIndicator = NO;
    self.listView.showsVerticalScrollIndicator = NO;
    // 刷新動画
    self.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.mj_header.lastUpdatedTimeLabel.hidden = YES;
    self.listView.mj_header = self.mj_header;
    self.listView.emptyDataSetSource = self;
    self.listView.emptyDataSetDelegate = self;
    if (self.disableAutoBeginRefresh) {
        [self.listView.mj_header beginRefreshing];
    }
    if ([self.listView isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView *)self.listView;
        if (tableView.style == UITableViewStylePlain) {
            tableView.tableFooterView = [[UIView alloc] init];
        }
    }
}

#pragma mark -

- (void)listViewBeginRefresh
{
    [self.listView.mj_header beginRefreshing];
}

- (void)listViewEndRefresh
{
    [self.listView.mj_header endRefreshing];
}

#pragma mark - ListViewProtocol
- (void)refreshData {
    
}

- (BOOL)disableAutoBeginRefresh {
    return NO;
}

- (UIScrollView *)listView {
    return nil;
}

- (void)setLoading:(BOOL)loading
{
    if (self.isLoading == loading) {
        return;
    }
    _loading = loading;
}

#pragma mark - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *imageStr = self.emtpyStateImageDict[@(self.emptyState)];
    UIImage * img = WSIMG(imageStr);
    return img;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    attributes[NSForegroundColorAttributeName] = UIColor.font_gray_484F5F;
    NSString *title = self.emtpyStateTextDict[@(self.emptyState)];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:WSEmptyStr(title) attributes:attributes];
    return attrString;
}


#pragma mark - DZNEmptyDataSetDelegate

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    scrollView.contentOffset = CGPointZero;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    BOOL show = !self.firstLoadPage;
    self.firstLoadPage = NO;
    
    return show;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    if(self.showLoading) {
        return NO;
    }
    if(self.emptyState == ListViewEmptyStateEmpty) {
        return YES;
    }
    return NO;
}

- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView {
    return self.isLoading;
}

- (void)emptyStartLoading {
    self.loading = YES;
    self.showLoading = YES;
    [self.listView reloadEmptyDataSet];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self refreshData];
    });
}

- (void)setEmptyStateTitle:(NSString *)title state:(ListViewEmptyState)state {
    self.emtpyStateTextDict[@(state)] = title;
}

- (void)setEmptyStateImage:(NSString *)image state:(ListViewEmptyState)state {
    self.emtpyStateImageDict[@(state)] = image;
}

- (NSMutableDictionary *)emtpyStateTextDict {
    if (_emtpyStateTextDict == nil) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        _emtpyStateTextDict = dict;
    }
    return _emtpyStateTextDict;
}

- (NSMutableDictionary *)emtpyStateImageDict {
    if (_emtpyStateImageDict == nil) {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[@(ListViewEmptyStateNoNetwork)] = @"img_jiazaishibai";
        dict[@(ListViewEmptyStateEmpty)] = @"img_zwss";
        _emtpyStateImageDict = dict;
    }
    return _emtpyStateImageDict;
}

@end
